package com.hunttown.mes.impl;

import com.hunttown.mes.convert.AnalysisManageRightMenuConverts;
import com.hunttown.mes.rpc.api.AnalysisManageRightMenuRpcService;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.domain.AnalysisManageRightMenuDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisManageRightMenuDTOQuery;
import com.hunttown.mes.service.AnalysisManageRightMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisManageRightMenuConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:05:42
 */
@Service
public class AnalysisManageRightMenuRpcServiceImpl implements AnalysisManageRightMenuRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisManageRightMenuRpcServiceImpl.class);

    private final AnalysisManageRightMenuService analysisManageRightMenuService;

    @Autowired
    public AnalysisManageRightMenuRpcServiceImpl(AnalysisManageRightMenuService analysisManageRightMenuService) {
        this.analysisManageRightMenuService = analysisManageRightMenuService;
    }

    @Override
    public AnalysisManageRightMenuDTO insert(AnalysisManageRightMenuDTO analysisManageRightMenuDTO) {
        try {
            return convert(analysisManageRightMenuService.insert(AnalysisManageRightMenuConverts.convert(analysisManageRightMenuDTO)));
        } catch (Exception e) {
            logger.error("AnalysisManageRightMenu create failed.", e);
            throw new RuntimeException("AnalysisManageRightMenu create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisManageRightMenuDTO analysisManageRightMenuDTO) {
        try {
            return analysisManageRightMenuService.updateInfoById(AnalysisManageRightMenuConverts.convert(analysisManageRightMenuDTO));
        } catch (Exception e) {
            logger.error("AnalysisManageRightMenu update by id failed.", e);
            throw new RuntimeException("AnalysisManageRightMenu update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisManageRightMenuDTOQuery analysisManageRightMenuDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisManageRightMenuDTOQuery.getW_id() != null && analysisManageRightMenuDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisManageRightMenuDTOQuery.getW_id());
            }

            if (analysisManageRightMenuDTOQuery.getIds() != null && !analysisManageRightMenuDTOQuery.getIds().equals("")) {
                query.put("ids", analysisManageRightMenuDTOQuery.getIds());
            }

            if (analysisManageRightMenuDTOQuery.getMenuNameEn() != null) {
                query.put("menuNameEn", analysisManageRightMenuDTOQuery.getMenuNameEn());
            }

            if (analysisManageRightMenuDTOQuery.getMenuPath() != null) {
                query.put("menuPath", analysisManageRightMenuDTOQuery.getMenuPath());
            }

            if (analysisManageRightMenuDTOQuery.getLastUpdatePeople() != null) {
                query.put("lastUpdatePeople", analysisManageRightMenuDTOQuery.getLastUpdatePeople());
            }

            if (analysisManageRightMenuDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", analysisManageRightMenuDTOQuery.getStopFlag());
            }

            if (analysisManageRightMenuDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisManageRightMenuDTOQuery.getDeleteFlag());
            }

            return analysisManageRightMenuService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisManageRightMenu update by query failed.", e);
            throw new RuntimeException("AnalysisManageRightMenu update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageRightMenuDTO selectById(Integer id) {
        try {
            return convert(analysisManageRightMenuService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisManageRightMenu select by id failed.", e);
            throw new RuntimeException("AnalysisManageRightMenu select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageRightMenuDTO selectObjByQuery(AnalysisManageRightMenuDTOQuery analysisManageRightMenuDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(analysisManageRightMenuService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisManageRightMenu select obj by query failed.", e);
            throw new RuntimeException("AnalysisManageRightMenu select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisManageRightMenuDTO> getListForPage(AnalysisManageRightMenuDTOQuery analysisManageRightMenuDTOQuery) {
        Query query = Query.page(analysisManageRightMenuDTOQuery.getPn(), analysisManageRightMenuDTOQuery.getPageSize());

        if (analysisManageRightMenuDTOQuery.getIds() != null && !analysisManageRightMenuDTOQuery.getIds().equals("")) {
            query.put("ids", analysisManageRightMenuDTOQuery.getIds());
        }

        if (analysisManageRightMenuDTOQuery.getLikeName() != null && !analysisManageRightMenuDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisManageRightMenuDTOQuery.getLikeName());
        }

        if (analysisManageRightMenuDTOQuery.getMenuNameEn() != "" && analysisManageRightMenuDTOQuery.getMenuNameEn() != null) {
            query.put("menuNameEn", analysisManageRightMenuDTOQuery.getMenuNameEn());
        }

        if (analysisManageRightMenuDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", analysisManageRightMenuDTOQuery.getStopFlag());
        }

        if (analysisManageRightMenuDTOQuery.getOrderBy() != null && analysisManageRightMenuDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisManageRightMenuDTOQuery.getOrderBy());
        }

        List<AnalysisManageRightMenuDTO> list = convert(analysisManageRightMenuService.selectByQuery(query));
        Page<AnalysisManageRightMenuDTO> page = Page.from(analysisManageRightMenuDTOQuery.getPn(), analysisManageRightMenuDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisManageRightMenuService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisManageRightMenuService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisManageRightMenu delete failed.", e);
            throw new RuntimeException("AnalysisManageRightMenu delete failed, Please order your code.", e);
        }
    }
}